home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / mtest / mtest.c < prev   
C/C++ Source or Header  |  1989-01-31  |  3KB  |  123 lines

  1. /*
  2.  * NAME
  3.  *      mtest - test memory allocation.
  4.  *
  5.  * COMPILATION
  6.  *    cc -o mtest mtest.c
  7.  *
  8.  * SYNOPSIS
  9.  *    mtest
  10.  *
  11.  * DESCRIPTION
  12.  *    mtest demonstrates that malloc is broken.  Specifically, that
  13.  *    malloc is not smart enough to use a piece of memory of exactly
  14.  *    the right size when it is available (unless it is at the end
  15.  *    of memory).
  16.  */
  17. #include <stdio.h>
  18. #include <sys/errno.h>
  19. #include <sys/types.h>
  20.  
  21. #define    BIGBUF        1000000    /* 1 MBytes */
  22. #define SMALLBUF    10240    /* 10 KBytes */
  23.  
  24. char    *malloc();
  25. caddr_t sbrk();
  26. extern int errno;
  27.  
  28. main()
  29. {
  30.     char    *buf1, *buf2;    /* buffer pointers */
  31.     caddr_t    topmem;        /* upper limit of memory */
  32.  
  33.     printf("beginning memory allocation test\n");
  34.     printf("All values are reported in decimal.\n");
  35.     topmem = sbrk(0);
  36.     printf("The end of memory is initially at %d\n\n",(int) topmem);
  37.  
  38.     /*
  39.      * Demonstrate that you can (sometimes) re-use memory which has
  40.      * been malloc'ed and subsequently free'd.
  41.      */
  42.     buf1 = malloc(BIGBUF);
  43.     if (buf1 == 0) {
  44.         perror("test");
  45.         errno = 0;
  46.         exit(1);
  47.         }
  48.     else {
  49.         printf("requested %d bytes of memory\n", BIGBUF);
  50.         topmem = sbrk(0);
  51.         printf("The end of memory is now at %d\n", (int) topmem);
  52.     }
  53.  
  54.     if (free(buf1) == 0) {
  55.         perror("test");
  56.         errno = 0;
  57.         exit(2);
  58.     }
  59.     else {
  60.         printf("%d bytes of memory freed\n", BIGBUF);
  61.         printf("There is now a %d byte piece of memory available.\n", BIGBUF);
  62.         topmem = sbrk(0);
  63.         printf("The end of memory is still at %d\n", (int) topmem);
  64.     }
  65.  
  66.     buf1 = malloc(BIGBUF);
  67.     if (buf1 == 0) {
  68.             perror("test");
  69.         errno = 0;
  70.         exit(1);
  71.     }
  72.     else {
  73.         printf("requested another %d bytes of memory -- ", BIGBUF);
  74.         printf("Should use available space.\n");
  75.         topmem = sbrk(0);
  76.         printf("The end of memory is still at %d\n\n", (int) topmem);
  77.     }
  78.  
  79.     /*
  80.      * Now demonstrate that if a block of free memory is surrounded
  81.      * by other chunks of memory, then a memory request of that size
  82.      * will NOT use the available piece.
  83.      */
  84.     buf2 = malloc(SMALLBUF);
  85.     if (buf2 == 0) {
  86.         perror("test");
  87.         errno = 0;
  88.         exit(1);
  89.     }
  90.         else {
  91.         printf("Requested %d bytes of memory.\n", SMALLBUF);
  92.         topmem = sbrk(0);
  93.         printf("The end of memory is now at %d\n", (int) topmem);
  94.     }
  95.  
  96.     if (free(buf1) == 0) {
  97.         perror("test");
  98.         errno = 0;
  99.         exit(2);
  100.     }
  101.     else {
  102.         printf("Freed %d bytes of memory.\n", BIGBUF);
  103.         printf("There is now a %d byte piece of memory available.\n", BIGBUF);
  104.         topmem = sbrk(0);
  105.         printf("The end of memory is still at %d\n", (int) topmem);
  106.     }
  107.  
  108.     buf1 = malloc(BIGBUF);
  109.     if (buf1 == 0) {
  110.         perror("test");
  111.             errno = 0;
  112.         exit(1);
  113.     }
  114.     else {
  115.         printf("Requested another %d bytes of memory -- ", BIGBUF);
  116.         printf("Should use available space.\n");
  117.         topmem = sbrk(0);
  118.         printf("WHOA!  The end of memory is now at %d\n",(int) topmem);
  119.     }
  120.  
  121.     exit(0);
  122. }
  123.